package demoMod.nanaki.cards.nanaki;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.animations.VFXAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.actions.common.DamageAllEnemiesAction;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.interfaces.NoteExplodeHook;
import demoMod.nanaki.powers.NotePower;
import demoMod.nanaki.utils.GetWaitAction;
import demoMod.nanaki.vfx.NoteFlashEffect;

import java.util.stream.Collectors;

public class Finale extends AbstractNanakiCard {
    public static final String ID = NanakiMod.makeID("Finale");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/Finale";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.ENEMY;

    private static final int COST = 0;

    public Finale() {
        super(ID, NAME, NanakiMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 1;
        this.selfRetain = true;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(2);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        boolean hasNoteBefore = m != null && m.hasPower(NotePower.POWER_ID);
        int noteAmountBefore = 0;
        if (hasNoteBefore) {
            noteAmountBefore = m.getPower(NotePower.POWER_ID).amount;
        }
        addToBot(new ApplyPowerAction(m, p, new NotePower(m, this.magicNumber)));
        int finalNoteAmountBefore = noteAmountBefore;
        addToBot(GetWaitAction.doAndWait(() -> {
            if (m == null || m.isDeadOrEscaped() || !hasNoteBefore) {
                return;
            }
            int explodeAmount = 25;
            if (AbstractDungeon.player.hasRelic(NanakiMod.makeID("SpellBottle"))) {
                explodeAmount += 9;
            }
            boolean shouldExplode = true;
            for (AbstractPower power : m.powers.stream().filter(abstractPower -> abstractPower instanceof NoteExplodeHook).collect(Collectors.toList())) {
                shouldExplode = shouldExplode && ((NoteExplodeHook) power).shouldExplode(m);
            }
            if (finalNoteAmountBefore + magicNumber >= explodeAmount && shouldExplode) {
                int damageAmount = explodeAmount;
                for (AbstractPower power : m.powers.stream().filter(abstractPower -> abstractPower instanceof NoteExplodeHook).collect(Collectors.toList())) {
                    damageAmount = ((NoteExplodeHook) power).modifyExplodeDamage(damageAmount);
                }
                if (p.hasRelic("IsaacExt:Birthright")) {
                    addToTop(new DamageAllEnemiesAction(p, DamageInfo.createDamageMatrix(damageAmount, true), DamageInfo.DamageType.THORNS, AbstractGameAction.AttackEffect.FIRE));
                } else {
                    addToTop(new DamageAction(m, new DamageInfo(m, damageAmount, DamageInfo.DamageType.THORNS), AbstractGameAction.AttackEffect.FIRE));
                }
                addToTop(new VFXAction(new NoteFlashEffect(m.hb.cX, m.hb.cY)));
            }
        }, 0.0F));
    }
}
